﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace CreateNetMessage
{
    public partial class FrmSetting : Form
    {
        public FrmSetting()
        {
            InitializeComponent();
        }

        private void FrmSetting_Load(object sender, EventArgs e)
        {
            btnClient.Tag = edtClient;
            btnGameServer.Tag = edtGameServer;
            btnLoginServer.Tag = edtLoginServer;
            LoadXml();
        }

        private void ButtonSelectFolderClick(object sender, EventArgs e)
        {
            var fbd = new FolderBrowserDialog();
            if (!string.IsNullOrEmpty(((TextBox)((Button)sender).Tag).Text))
                fbd.SelectedPath = ((TextBox) ((Button) sender).Tag).Text;

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                ((TextBox) ((Button) sender).Tag).Text = fbd.SelectedPath;
            }
        }

        private void LoadXml()
        {
            if (File.Exists(SaveFile))
            {
                var ser = new XmlSerializer(typeof(CodeFilePath));
                using (var fs = new FileStream(SaveFile, FileMode.Open))
                {
                    codeFilePathBindingSource.DataSource = ser.Deserialize(fs) as CodeFilePath;
                    fs.Close();
                }
            }
            else
            {
                codeFilePathBindingSource.DataSource = new CodeFilePath();
            }
        }

        private void BtnSaveClick(object sender, EventArgs e)
        {
            if (codeFilePathBindingSource.DataSource != null)
            {
                var cfp = codeFilePathBindingSource.DataSource as CodeFilePath;

                var ser = new XmlSerializer(typeof(CodeFilePath));
                using (var fs = new FileStream(SaveFile, FileMode.Create))
                {
                    ser.Serialize(fs, cfp);
                    fs.Close();
                }
            }
        }

        private static string SaveFile
        {
            get { return "CodeFolderSetting.xml"; }
        }
    }

    public class CodeFilePath
    {
        public string ClientPath { get; set; }

        public string GameServerPath { get; set; }

        public string LoginServerPath { get; set; }
    }
}
